﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using xiaodingmarket_application.Interfaces;
using xiaodingmarket_application.Service;
using xiaodingmarket_application.ViewModel;
using xiaodingmarket_Domain_core.Bus;
using xiaodingmarket_Domain_core.Notificatiions;

namespace xiaodingmarket_service.Controllers.OrderController
{
    public class OrderController : ApiController
    {

        private readonly IOrderService _orderService;
        public OrderController(IOrderService orderService,INotificationHandler<DomainNotification> notifications, IMediatorHandler mediator) : base(notifications, mediator)
        {
            _orderService = orderService;
        }
        /// <summary>
        /// 根据kind大类，用户id，订单状态Condition，who的意思是0是创建订单的人，1是购买人
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="kind"></param>
        /// <param name="Condition"></param>
        /// <param name="who"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("Order")]
        [AllowAnonymous]
        //[Authorize(Policy = "CanBasicoperation")]
        public IActionResult Get(Guid userid, int kind, int Condition, int who)
        {
            return Response(_orderService.GetAll(userid,kind,Condition,who));
        }
        /// <summary>
        /// 根据订单id进行查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("order/{id:guid}")]
        [AllowAnonymous]
        //[Authorize(Policy = "CanBasicoperation")]
        public IActionResult Get(Guid id)
        {
            var orderViewModel = _orderService.GetById(id);

            return Response(orderViewModel);
        }
        /// <summary>
        /// 当卖家确认卖给谁，自动生成订单
        /// </summary>
        /// <param name="orderViewMode"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        //[Authorize(Policy = "CanBasicoperation")]
        [Route("order")]
        public IActionResult Post([FromBody]OrderViewModel orderViewMode)
        {
            if (!ModelState.IsValid)
            {
                NotifyModelStateErrors();
                return Response(orderViewMode);
            }

            _orderService.Create(orderViewMode);

            return Response(orderViewMode);
        }

        /// <summary>
        /// 订单状态改变的时候修改订单
        /// </summary>
        /// <param name="orderViewMode"></param>
        /// <returns></returns>
        [HttpPut]
        [AllowAnonymous]
        //[Authorize(Policy = "CanBasicoperation")]
        [Route("order")]
        public IActionResult Put([FromBody]OrderViewModel orderViewMode)
        {
            if (!ModelState.IsValid)
            {
                NotifyModelStateErrors();
                return Response(orderViewMode);
            }

            _orderService.Update(orderViewMode);

            return Response(orderViewMode);
        }
        /// <summary>
        /// 订单软删除（一般是退货，纠纷等情况上报后做的删除）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        [AllowAnonymous]
        //[Authorize(Policy = "CanBasicoperation")]
        [Route("customer-management")]
        public IActionResult Delete(Guid id)
        {
            _orderService.Remove(id);

            return Response();
        }

    }
}
